<template>
  <div>
    <el-card
      shadow="never"
      :body-style="{ padding: '0px' }"
      :page-size="pageSize"
      :total="totalPage"
      :current-page="pageIndex"
      @current-change="currentChangeHandle"
    >
      <el-form class="p-4" inline>
        <el-form-item>
          <el-select
            v-model="userId"
            clearable
            :placeholder="$t('system.log.operator_name')"
          >
            <el-option
              v-for="item in userPage"
              :key="item.userId"
              :label="item.name"
              :value="item.userId"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            clearable
            v-model="eventCode"
            :placeholder="$t('system.log.action')"
          >
            <el-option
              v-for="item in eventCodePage"
              :key="item.eventCode"
              :label="item.label"
              :value="item.eventCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            maxlength="64"
            v-model="eventDesc"
            @blur="() => (eventDesc = eventDesc.trim())"
            :placeholder="$t('system.log.event_desc')"
          />
        </el-form-item>
        <el-form-item>
          
          <el-date-picker
            v-model="bindDate"
            type="daterange"
            value-format="x"
            :start-placeholder="$t('common.start_time')"
            :end-placeholder="$t('common.end_time')"
            :picker-options="pickerOptions"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="w-8"
            @click="fetchData((pageIndex = 1))"
          >
            <svg-icon icon-class="search"/>
          </el-button>
        </el-form-item>
        <!-- <el-form-item>
          <el-tooltip :content="$t('common.btn_tips.export')" placement="top">
            <el-button
              size="medium"
              type="primary"
              class="align-middle p-2"
              :loading="authLoading"
              @click="exportAuthLog"
            >
              <i class="el-icon-download" />
            </el-button>
          </el-tooltip>
        </el-form-item> -->
      </el-form>

      <el-table v-loading="listLoading" :show-overflow-tooltip="true" :data="tableData" stripe>
        <el-table-column :label="$t('system.log.operator_name')" prop="userName" align="center" />

        <el-table-column :label="$t('system.log.action')" align="center" prop="eventCode">
          <template v-slot="scope">
            <span style="margin-left: 10px">{{
              $t('system.authorization_log_code.' + scope.row.eventCode)
            }}</span>
          </template>
        </el-table-column>

        <el-table-column :label="$t('system.log.event_desc')" align="center" prop="eventDesc" />

        <el-table-column :label="$t('common.create_time')" prop="creTime" align="center">
          <template v-slot="scope">
            <div :title="timeFilter(scope.row.creTime)">{{ dateFilter(scope.row.creTime) }}</div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { getAllUser, getUserPage } from '@/api/user'
import { getAuthLogList, getAuthActionList } from '@/api/log'
import { timeFilter, dateFilter } from '@/filters/index'
import { parseTime } from '@/utils/index'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'
import store from '@/store'
import axios from 'axios'

export default {
  name: 'AuthorizationLog',
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: 'Invalid',
        1: 'Valid'
      }
      return statusMap[status]
    },
    timeFilter,
    dateFilter
  },

  data() {
    return {
      authLoading: false,
      cur_page: 1,
      cur_pageSize: 10,
      userPage: [],
      userId: undefined,
      eventCodePage: [],
      eventCode: undefined,
      eventDesc: undefined,
      bindDate: [],
      orgId: '',
      tableData: [],
      listLoading: false,
      selectedData: [],
      paginationShow: true,
      settings: {
        conditions: [],
        columns: []
      },
      queryOrg: {},
      condition: '',
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      addRoleVisible: false,
      operationType: '',
      editRoleId: '',
      pickerOptions: {
        onPick:time => {
          //当第一时间选中才设置禁用
          if (time.minDate && !time.maxDate) {
            this.timeOptionRange = time.minDate;
          }
          if (time.maxDate) {
            this.timeOptionRange = null;
          }
        },
        disabledDate:time => {
          let timeOptionRange = this.timeOptionRange;
          let secondNum = 60 * 60 * 24 * 3*30 * 1000;
          if (timeOptionRange) {
            return time.getTime() > timeOptionRange.getTime() + secondNum || time.getTime() < timeOptionRange.getTime() - secondNum;
          }
        }
      }
    }
  },
  mounted() {
    this.initDate()
    this.fetchData()
  },
  methods: {
    timeFilter,
    dateFilter,
    initDate() {
      const now = Date.now()
      const endTime = new Date(now)
      const startTime = new Date(now - 7 * 24 * 3600 * 1000)
      this.bindDate[0] = startTime.getTime()
      this.bindDate[1] = endTime.getTime()
      getAllUser({ condition: '', platCode: 'PASS' })
        .then((res) => {
          this.listLoading = false
          this.userPage = res
          console.log(this.userPage)
        })
        .catch((e) => {
          this.listLoading = false
          this.$confirmError(e)
        })
      getAuthActionList({
        orgId: this.$store.getters.userInfo.orgId
      })
        .then((res) => {
          console.log(res)
          this.eventCodePage = []
          res.forEach((item) => {
            const eventItem = {}
            eventItem.eventCode = item
            eventItem.label = this.$t('system.authorization_log_code.' + item)
            this.eventCodePage.push(eventItem)
          })
          console.log(JSON.stringify(this.eventCodePage))
        })
        .catch((e) => {
          this.listLoading = false
          this.$confirmError(e)
        })
    },
    fetchData() {
      this.listLoading = true
      let startTime, endTime
      if (
        this.bindDate === null ||
        this.bindDate === undefined ||
        this.bindDate[0] === null ||
        this.bindDate[0] === undefined ||
        this.bindDate[1] === null ||
        this.bindDate[1] === undefined
      ) {
        this.$message.error(this.$t('system.log.select_time_range'))
        this.listLoading = false
        return false
      }
      if (this.bindDate) {
        this.bindDate.forEach((val, index) => {
          if (index === 0) {
            startTime = Math.round(val / 1000)
          } else if (index === 1) {
            endTime = Math.round(val / 1000) + 24 * 60 * 60
          }
        })
      }
      getAuthLogList({
        pageNo: this.pageIndex,
        pageSize: this.pageSize,
        orgId: this.$store.getters.userInfo.orgId,
        userId: this.userId,
        eventCode: this.eventCode,
        eventDesc: this.eventDesc,
        startTime: startTime,
        endTime: endTime
      })
        .then((res) => {
          this.listLoading = false
          this.tableData = res.data
          this.totalPage = res.rows
          console.log(res.data)
        })
        .catch((e) => {
          this.listLoading = false
          this.$confirmError(e)
        })


    },
    sizeChangeHandle(val) {
      this.pageSize = val
      this.fetchData()
    },
    currentChangeHandle(val) {
      this.pageIndex = val
      this.fetchData()
    },
    showHeader(label) {
      var list = this.settings['columns']
      return list ? list.includes(label) : false
    },
    showFormItem(label) {
      var list = this.settings['conditions']
      return list ? list.includes(label) : false
    }
  }
}
</script>

<style lang="scss">
.terminal-list {
  .el-table .cell {
    /* height: 30px !important; */
  }
}
</style>

<style lang="scss" scoped>
.query-button {
  margin-left: 30px;
}
.svg-container {
  cursor: pointer;
  margin-top: 6px;
  margin-left: 20px;
  font-size: 24px;
  display: inline-block;
}

.pagination-wrapper {
  text-align: right;
}
</style>
